import 'package:flutter/material.dart';
import 'package:flutter_boost/flutter_boost.dart';
import 'package:get/get_navigation/src/router_report.dart';

class AppLifecycleObserver extends GlobalPageVisibilityObserver {

  @override
  void onBackground(Route route) {
    super.onBackground(route);
    _printLog('AppLifecycleObserver - ${route.settings.name} onBackground');
  }

  @override
  void onForeground(Route route) {
    super.onForeground(route);
    _printLog('AppLifecycleObserver - ${route.settings.name} onForeground');
  }

  @override
  void onPagePush(Route route) {
    super.onPagePush(route);
    // 因为在项目中使用flutter_boost管理页面路由，所以Getx失去了页面的生命周期的控制，导致在页面关闭时无法感知页面的pop，从而导致该页面对应controller无法销毁。
    // 其中最优解就是在外部监听页面的生命周期，手动去释放。
    RouterReportManager.reportCurrentRoute(route);
    _printLog('AppLifecycleObserver - ${route.settings.name} onPagePush');
  }

  @override
  void onPagePop(Route route) {
    super.onPagePop(route);
    RouterReportManager.reportRouteDispose(route);
    _printLog('AppLifecycleObserver - ${route.settings.name} onPagePop');
  }

  @override
  void onPageHide(Route route) {
    super.onPageHide(route);
    _printLog('AppLifecycleObserver - ${route.settings.name} onPageHide');
  }

  @override
  void onPageShow(Route route) {
    super.onPageShow(route);
    _printLog('AppLifecycleObserver - ${route.settings.name} onPageShow');
  }

  void _printLog(String content) {
    debugPrint(content);
  }
}